/*
 * Copyright(C) 2017 Hangzhou Differsoft Co., Ltd. All rights reserved.
 */
package com.dlc.shop.jky.common.service;

import com.linchuang.seneca.jky.bizdata.core.BaseNullRequestBizData;
import com.linchuang.seneca.jky.bizdata.core.BaseRequestBizData;

import com.linchuang.seneca.jky.bizdata.vend.goodssourcecreate.GoodsSourceCreate;
import com.linchuang.seneca.jky.exception.JkyException;
import com.linchuang.seneca.jky.response.JackyunResponse;
import com.linchuang.seneca.jky.utils.JackyunOpenHttpUtils;
import com.linchuang.seneca.jky.utils.JsonUtils;
import com.dlc.shop.common.jky.JkyMp;
import jdk.dynalink.linker.LinkerServices;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 请求吉客云开放平台服务。
 *
 * @author HGW
 * @since 2018-08-29  14:46:43
 */

@Service
public class JackyunOpenClientService {
    /**
     * 请求开放平台服务。
     *
     * @param method  开放接口方法名
     * @param version 开放接口版本号(null表示默认)
     * @param bizData 请求业务数据
     * @return OpenResponse返回对象
     */
    public JackyunResponse call(JkyMp jkyMp, BaseRequestBizData bizData) {
        //接口返回值
        JackyunResponse response = null;
        //返回值字符串
        String strResponse;
        strResponse = null;
        try {
            //请求吉客云开放接口。
            //String appKey, String appSecret, String token, String gateway,
            strResponse = JackyunOpenHttpUtils.post(jkyMp.getAppKey(),jkyMp.getAppSecret(),jkyMp.getGateway(),jkyMp.getVersion(), bizData);
        } catch (JkyException ex) {
            response = new JackyunResponse();
            response.onFail(ex.getMessage(), "CLIENT_EXCEPTION");
            return response;
        }catch (Exception ex) {
            response = new JackyunResponse();
            response.onFail(ex.getMessage(), "CLIENT_EXCEPTION");
            return response;
        }
        return JsonUtils.deJson(strResponse, JackyunResponse.class);
    }
    /**
     * 请求开放平台服务。
     *
     * @param method  开放接口方法名
     * @param version 开放接口版本号(null表示默认)
     * @param bizData 请求业务数据
     * @return OpenResponse返回对象
     */
    public JackyunResponse call(String method, JkyMp jkyMp, List<GoodsSourceCreate> list ) {
        //接口返回值
        JackyunResponse response = null;
        //返回值字符串
        String strResponse;
        strResponse = null;
        try {
            //请求吉客云开放接口。
            //String appKey, String appSecret, String token, String gateway,
            strResponse = JackyunOpenHttpUtils.post(method,jkyMp.getAppKey(),jkyMp.getAppSecret(),jkyMp.getGateway(),jkyMp.getVersion(), list);
        } catch (JkyException ex) {
            response = new JackyunResponse();
            response.onFail(ex.getMessage(), "CLIENT_EXCEPTION");
            return response;
        }catch (Exception ex) {
            response = new JackyunResponse();
            response.onFail(ex.getMessage(), "CLIENT_EXCEPTION");
            return response;
        }
        return JsonUtils.deJson(strResponse, JackyunResponse.class);
    }
}
